package chapter12.eg1;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import java.net.InetSocketAddress;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLException;

public class SecureChatServer extends ChatServer {

  private final SslContext context;

  public SecureChatServer(SslContext context) {
    this.context = context;
  }

  @Override
  protected ChannelHandler createInitializer(ChannelGroup channelGroup) {
    return new SecureChatServerInitializer(channelGroup, context);
  }

  public static void main(String[] args) throws Exception {
    SelfSignedCertificate certificate = new SelfSignedCertificate();
    SslContext context = SslContext
        .newServerContext(certificate.certificate(), certificate.privateKey());
    final SecureChatServer endpoint = new SecureChatServer(context);
    ChannelFuture future = endpoint.start(new InetSocketAddress(9999));
    Runtime.getRuntime().addShutdownHook(new Thread(() -> endpoint.destroy()));
    future.channel().closeFuture().syncUninterruptibly();
  }
}
